//
//  FeedbackViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/13.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "FeedbackViewController.h"
#import "sys/sysctl.h"

@interface FeedbackViewController ()
@property (nonatomic, strong)UITextView *feedback;//反馈内容
@property (nonatomic, strong)UIBarButtonItem *rightButtonItem;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"意见和反馈";
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    
    self.rightButtonItem  = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(Actionright)];
    self.navigationItem.rightBarButtonItem = self.rightButtonItem;//调用↑2个方法
    
    UILabel *Label_prompt = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, DeviceWidth-40, 20)];
    Label_prompt.text = @"吐槽和建议";
    Label_prompt.textColor = [UIColor darkGrayColor];
    Label_prompt.font = [UIFont fontWithName:FontName size:15];//改字体和大小
    [self.view addSubview:Label_prompt];
    
    //反馈内容
    self.feedback = [[UITextView alloc]initWithFrame:CGRectMake(20, 100, DeviceWidth-40, 100)];
    self.feedback.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    [self.feedback.layer setBorderColor:[[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor];
    [self.feedback.layer setBorderWidth:0.5];
    self.feedback.layer.masksToBounds = YES;//开启圆角
    [self.feedback.layer setCornerRadius:5];
    [self.view addSubview:self.feedback];
    
    UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.feedback.frame), DeviceWidth-40, 20)];
    phone.text = [NSString stringWithFormat:@"型号:%@",[self doDevicePlatform]];
    phone.textColor = [UIColor darkGrayColor];
    phone.font = [UIFont fontWithName:FontName size:15];//改字体和大小
    [self.view addSubview:phone];
    
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    UILabel *Version = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(phone.frame), DeviceWidth-40, 20)];
    Version.text = [NSString stringWithFormat:@"版本号:%@",[infoDict objectForKey:@"CFBundleShortVersionString"]];
    Version.textColor = [UIColor darkGrayColor];
    Version.font = [UIFont fontWithName:FontName size:15];//改字体和大小
    [self.view addSubview:Version];
    
    
    //键盘回收
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyBorad)]];
}

//键盘回收
-(void)returnKeyBorad
{
    [self.view endEditing:YES];
}

//提交反馈
-(void)Actionright{
    self.rightButtonItem.enabled = NO;
    [self.view endEditing:YES];
    NSString *advise = self.feedback.text;
    // 发送反馈
    if ([advise isEqualToString:@""]) {
        [Utils alert:@"请填写您的建议"];
        return;
    }
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

    
    MPFeedback *feedback = [MPFeedback object];
    feedback.user = [MPUser currentUser];
    feedback.advise = advise;
    feedback.version = [infoDict objectForKey:@"CFBundleShortVersionString"];
    feedback.sys_info = [self doDevicePlatform];

    feedback.status = MPFeedbackStatus_NORMAL;
    [feedback saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
         self.rightButtonItem.enabled = YES;
        if (succeeded) {
            [HUD Prompt:@"谢谢您的反馈!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [HUD Prompt:[NSString stringWithFormat:@"%@",@"网络异常"] ];
        }
    }];
}

- (NSString*) doDevicePlatform
{
    size_t size;
    int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    
    if ([platform isEqualToString:@"iPhone1,1"]) {
        
        platform = @"iPhone";
        
    } else if ([platform isEqualToString:@"iPhone1,2"]) {
        
        platform = @"iPhone 3G";
        
    } else if ([platform isEqualToString:@"iPhone2,1"]) {
        
        platform = @"iPhone 3GS";
        
    } else if ([platform isEqualToString:@"iPhone3,1"]||[platform isEqualToString:@"iPhone3,2"]||[platform isEqualToString:@"iPhone3,3"]) {
        
        platform = @"iPhone 4";
        
    } else if ([platform isEqualToString:@"iPhone4,1"]) {
        
        platform = @"iPhone 4S";
        
    } else if ([platform isEqualToString:@"iPhone5,1"]||[platform isEqualToString:@"iPhone5,2"]) {
        
        platform = @"iPhone 5";
        
    }else if ([platform isEqualToString:@"iPhone5,3"]||[platform isEqualToString:@"iPhone5,4"]) {
        
        platform = @"iPhone 5C";
        
    }else if ([platform isEqualToString:@"iPhone6,2"]||[platform isEqualToString:@"iPhone6,1"]) {
        
        platform = @"iPhone 5S";
        
    }else if ([platform isEqualToString:@"iPhone7,1"]) {
        
        platform = @"iPhone 6 Plus";
        
    }else if ([platform isEqualToString:@"iPhone7,2"]) {
        
        platform = @"iPhone 6";
        
    }else if ([platform isEqualToString:@"iPod4,1"]) {
        
        platform = @"iPod touch 4";
        
    }else if ([platform isEqualToString:@"iPod5,1"]) {
        
        platform = @"iPod touch 5";
        
    }else if ([platform isEqualToString:@"iPod3,1"]) {
        
        platform = @"iPod touch 3";
        
    }else if ([platform isEqualToString:@"iPod2,1"]) {
        
        platform = @"iPod touch 2";
        
    }else if ([platform isEqualToString:@"iPod1,1"]) {
        
        platform = @"iPod touch";
        
    } else if ([platform isEqualToString:@"iPad3,2"]||[platform isEqualToString:@"iPad3,1"]) {
        
        platform = @"iPad 3";
        
    } else if ([platform isEqualToString:@"iPad2,2"]||[platform isEqualToString:@"iPad2,1"]||[platform isEqualToString:@"iPad2,3"]||[platform isEqualToString:@"iPad2,4"]) {
        
        platform = @"iPad 2";
        
    }else if ([platform isEqualToString:@"iPad1,1"]) {
        
        platform = @"iPad 1";
        
    }else if ([platform isEqualToString:@"iPad2,5"]||[platform isEqualToString:@"iPad2,6"]||[platform isEqualToString:@"iPad2,7"]) {
        
        platform = @"ipad mini";
        
    } else if ([platform isEqualToString:@"iPad3,3"]||[platform isEqualToString:@"iPad3,4"]||[platform isEqualToString:@"iPad3,5"]||[platform isEqualToString:@"iPad3,6"]) {
        
        platform = @"ipad 3";
        
    }else{
        
        platform = @"platform";

    }
    
    return platform;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
